#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void printt(int * a, int len){
    for(int i=0;i<len;i++){
        printf("%d\n",a[i]);
    }
}

// 打印任意类型的数组
void printAllArr(void * pArray, int eleSize, int len, void(*myprint)(void *)){
    char * p=pArray;
    for(int i=0;i<len;i++){
        // 获取数组中每个元素的首地址
        char * eleAddr = p+ eleSize *i;
        // printf("%d\n", *(int*)eleAddr);
        // 这里打印的 需要交给用户确定

        myprint(eleAddr);
    }
}

void myprint(void * data){
    int * num = data;
    printf("%d\n", *num);
}





void test1(){
    int arr[5] = {1,2,3,4,5};
    int len = sizeof(arr)/sizeof(int);
    printAllArr(arr, sizeof(int), len, myprint);
}

struct Person{
    char name[64];
    int age;
};

void myPrintPer(void * data){
    struct Person * p = data;
    printf("姓名：%s\t 年龄:%d", p->name,p->age);
}


//查找数组中的元素是否存在
//@param1 
int findArrayEle(void * pArray, int eleSize, int len, void * data, int(*myCompare)(void *,void *)){
    char * p = pArray;
    for(int i=0;i<len;i++){
        // 每个元素的首地址
        char * eleAddr = p + eleSize*i;
        if(myCompare(eleAddr,data)){
            return 1;
        }
    }

    return 0;
}

int myCompare(void * data1, void * data2){
    struct Person * p1 = data1;
    struct Person * p2 = data2;
    if( strcmp(p1->name, p2->name) == 0 && p1->age == p2->age){
        return 1;
    }

    return 0;
}


void test2(){
    struct Person personArr[] = {
        {"aaa",10},
        {"bbb",20},
    };
    int len = sizeof(personArr)/sizeof(struct Person);
    printAllArr(personArr, sizeof(struct Person), len, myPrintPer);

    // 查找数组中的元素是否存在
    struct Person p = {"bbb", 20};
    int ret = findArrayEle(personArr, sizeof(struct Person), len, &p);

    if (ret){
        printf("找到了");
    }else{
        printf("妹");
    }

}








void main(){

    test1();

    test2();

}